/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.google.code.peersim.pastry.protocol;

import peersim.config.Configuration;

/**
 * Immutable class for representing resources. A resource has a {@link PastryId}
 * and a payload.
 *
 * @author frusso
 * @version 0.1
 * @since 0.1
 */
public class PastryResource<T> {

  /**
   * Pastry's <code>b</code> parameter.
   */
  private static final int b = Configuration.getInt("protocol.pastry.b");
  /**
   * The Pastry identifier assigned to this resource.
   */
  private final PastryId resourceId;
  /**
   * The actual resource.
   */
  private final T resource;

  /**
   * Constructor.
   *
   * @param resource The payload
   */
  public PastryResource(T resource) {
    resourceId = PastryId.newId(b, null);
    this.resource = resource;
  }

  /**
   * Returns the resource id.
   * @return The resource id
   */
  public final PastryId getResourceId() {
    return resourceId;
  }

  /**
   * Returns the resource.
   * @return The resource
   */
  public final T getResource() {
    return resource;
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public boolean equals(Object obj) {
    if(!(obj instanceof PastryResource))
      return false;
    return this.resourceId.equals(((PastryResource)obj).resourceId);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public int hashCode() {
    return resourceId.hashCode();
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public String toString() {
    return "["+resourceId+", "+resource+"]";
  }
}